package com.demo.business.order;

import com.tiangong.framework.annotation.OpenApi;
import com.tiangong.framework.bo.CDO;
import com.tiangong.framework.bo.Return;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @description: 定单表
 * @create: 2022-02-25 23:30:08
 **/
@Service("order")
@Slf4j
public class OrderService{

    @Resource
    OrderDao orderDao;

    /**
     * 添加定单表,获取lOrderId数据
     *
     * @param cdoRequest - lOrderId:订单id
     *                   - sOrderPostfix:订单id后缀
     *                   - lUserId:用户id
     *                   - lBizId:业务id
     *                   - nPayFee:支付金额
     *                   - nPaydisFee:支付优惠金额
     *                   - nPayType:支付方式（1：支付宝，2：微信，3：钱包支付 4：applepay ）
     *                   - sPayName:支付方式
     *                   - s3rdUserId:第三方用户信息
     *                   - s3rdOrderId:第三方订单id
     *                   - nOrderType:定单类型：1 会员商品,2、促销商品 
     *                   - nStatus:状态：0未支付,1支付成功
     *                   - dCreateTime:创建时间
     *                   - dUpdateTime:更新时间
     * @return
     * @author 
     * @create: 2022-02-25 23:30:08
     */
    @OpenApi
    public Return addOrder(CDO cdoRequest) {
        orderDao.addOrder(cdoRequest);
        CDO cdoResult = new CDO();
        if (cdoRequest.containsKey("lOrderId")) {
            cdoResult.set("lOrderId", cdoRequest.get("lOrderId"));
        }
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据lOrderId，更新定单表数据
     *
     * @param cdoRequest - lOrderId:订单id
     *                   - sOrderPostfix:订单id后缀
     *                   - lUserId:用户id
     *                   - lBizId:业务id
     *                   - nPayFee:支付金额
     *                   - nPaydisFee:支付优惠金额
     *                   - nPayType:支付方式（1：支付宝，2：微信，3：钱包支付 4：applepay ）
     *                   - sPayName:支付方式
     *                   - s3rdUserId:第三方用户信息
     *                   - s3rdOrderId:第三方订单id
     *                   - nOrderType:定单类型：1 会员商品,2、促销商品 
     *                   - nStatus:状态：0未支付,1支付成功
     *                   - dCreateTime:创建时间
     *                   - dUpdateTime:更新时间
     * @return
     * @author 
     * @create: 2022-02-25 23:30:08
     */
    @OpenApi
    public Return updateOrderById(CDO cdoRequest) {
        int affectedRows = orderDao.updateOrderById(cdoRequest);
        CDO cdoResult = new CDO();
        cdoResult.set("affectedRows", affectedRows);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据lOrderId，删除定单表数据
     *
     * @param cdoRequest - lOrderId:订单id
     * @return
     * @author 
     * @create: 2022-02-25 23:30:08
     */
    @OpenApi
    public Return deleteOrderById(CDO cdoRequest){
        int affectedRows = orderDao.deleteOrderById(cdoRequest.getLong("lOrderId"));
        CDO cdoResult = new CDO();
        cdoResult.set("affectedRows", affectedRows);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据条件，删除定单表数据
     *
     * @param cdoRequest - lUserId:用户id
     * @return
     * @author 
     * @create: 2022-02-25 23:30:08
     */
    //@OpenApi
    public Return deleteOrder(CDO cdoRequest) {
        int affectedRows = orderDao.deleteOrder(cdoRequest);
        CDO cdoResult = new CDO();
        cdoResult.set("affectedRows", affectedRows);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据ID，获取定单表数据
     *
     * @param cdoRequest - lOrderId:订单id
     * @return
     * @author 
     * @create: 2022-02-25 23:30:08
     */
    @OpenApi
    public Return getOrderById(CDO cdoRequest) {
        CDO cdoMapperResult = orderDao.getOrderById(cdoRequest.getLong("lOrderId"));
        return Return.valueOf(cdoMapperResult);
    }

    /**
     * 根据混合条件，获取定单表数据
     *
     * @param cdoRequest - lOrderId:订单id
     *                   - lUserId:用户id
     * @return
     * @author 
     * @create: 2022-02-25 23:30:08
     */
    @OpenApi
    public Return getOrder(CDO cdoRequest) {
        CDO cdoMapperResult = orderDao.getOrder(cdoRequest);
        return Return.valueOf(cdoMapperResult);
    }

    /**
     * 根据非空条件，获取定单表数据列表
     *
     * @param cdoRequest - lUserId:用户id
     * @return
     * @author 
     * @create: 2022-02-25 23:30:08
     */
    @OpenApi
    public Return getOrderList(CDO cdoRequest) {
        Integer pageIndex = cdoRequest.containsKey("pageIndex") ? cdoRequest.getInteger("pageIndex") : 0;
        int recordSize = cdoRequest.containsKey("recordSize") ? cdoRequest.getInteger("recordSize") : 10;
        cdoRequest.set("recordFrom", pageIndex * recordSize);
        cdoRequest.set("recordSize", recordSize);
        List<CDO> cdoMapperResultList = orderDao.getOrderList(cdoRequest);
        boolean isEndPage = false;
        if (cdoMapperResultList.size() < recordSize) {
            isEndPage = true;
        }
        CDO cdoResult = new CDO();
        cdoResult.set("isEndPage", isEndPage);
        cdoResult.set("datalist", cdoMapperResultList);
//        int total = orderDao.getOrderListCount(cdoRequest);
//        cdoResult.set("total", total);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据混合条件，获取定单表数据列表
     *
     * @param cdoRequest - lOrderId:订单id
     *                   - lUserId:用户id
     * @return
     * @author 
     * @create: 2022-02-25 23:30:08
     */
    @OpenApi
    public Return getOrderListByCondition(CDO cdoRequest) {
        Integer pageIndex = cdoRequest.containsKey("pageIndex") ? cdoRequest.getInteger("pageIndex") : 0;
        int recordSize = cdoRequest.containsKey("recordSize") ? cdoRequest.getInteger("recordSize") : 10;
        cdoRequest.set("recordFrom", pageIndex * recordSize);
        cdoRequest.set("recordSize", recordSize);
        List<CDO> cdoMapperResultList = orderDao.getOrderListByCondition(cdoRequest);
        boolean isEndPage = false;
        if (cdoMapperResultList.size() < recordSize) {
            isEndPage = true;
        }
        CDO cdoResult = new CDO();
        cdoResult.set("isEndPage", isEndPage);
        cdoResult.set("datalist", cdoMapperResultList);
//        int total = orderDao.getOrderListByConditionCount(cdoRequest);
//        cdoResult.set("total", total);
        return Return.valueOf(cdoResult);
    }
}
